package com.example.movie.ui.movie.adapter

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
import android.text.style.AbsoluteSizeSpan
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.RequiresApi
import androidx.recyclerview.widget.RecyclerView
import com.example.movie.R
import com.example.movie.data.cinema.model.CinemaMovieItemInfo
import com.example.movie.ui.cinema.MoviePlanActivity
import com.example.movie.ui.cinema.SeatTableActivity
import kotlinx.android.synthetic.main.cinema_plan_movie.view.*

class PlanMovieListAdapter(
    private val items: List<CinemaMovieItemInfo>,
    private val activity: MoviePlanActivity
) : RecyclerView.Adapter<PlanMovieListAdapter.CinemaMovieListViewHolder>() {

    class CinemaMovieListViewHolder(cinemaLayout: View) :
        RecyclerView.ViewHolder(cinemaLayout)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CinemaMovieListViewHolder {
        val movieDateLayout =
            LayoutInflater.from(parent.context)
                .inflate(R.layout.cinema_plan_movie, parent, false)
        return CinemaMovieListViewHolder(movieDateLayout)
    }

    @RequiresApi(Build.VERSION_CODES.N)
    override fun onBindViewHolder(holder: CinemaMovieListViewHolder, position: Int) {
        with(holder.itemView){

            with(SpannableStringBuilder(items[position].startTime + "\n" + items[position].endTime + "  散场")){
                items[position].startTime?.let {
                    this@with.setSpan(
                        AbsoluteSizeSpan(60),
                        0,
                        it.length,
                        SPAN_EXCLUSIVE_EXCLUSIVE
                    )
                }
                items[position].startTime?.let {
                    this@with.setSpan(
                        AbsoluteSizeSpan(40),
                        it.length,
                        this@with.length,
                        SPAN_EXCLUSIVE_EXCLUSIVE
                    )
                }
                duration.text = this@with
            }

            with(SpannableStringBuilder(items[position].movieHall + "\n" + items[position].hallType)){
                items[position].movieHall?.let {
                    this@with.setSpan(
                        AbsoluteSizeSpan(50),
                        0,
                        it.length,
                        SPAN_EXCLUSIVE_EXCLUSIVE
                    )
                }
                items[position].movieHall?.let {
                    this@with.setSpan(
                        AbsoluteSizeSpan(40),
                        it.length,
                        this@with.length,
                        SPAN_EXCLUSIVE_EXCLUSIVE
                    )
                }
                movie_hall.text = this@with
            }

            with(SpannableString(items[position].price + "元")){
                items[position].price?.let {
                    this@with.setSpan(
                        AbsoluteSizeSpan(50),
                        0,
                        it.length,
                        SPAN_EXCLUSIVE_EXCLUSIVE
                    )
                }
                price.text = this@with
            }

            buy_tickets.setOnClickListener {
                with(Bundle()){
                    this.putInt("planId",items[position].planId)
                    val intent = Intent(activity, SeatTableActivity::class.java)
                    intent.putExtras(this)
                    activity.startActivity(intent)
                }
            }
            
        }
    }

    override fun getItemCount() = items.size

}